﻿using NUnit.Framework;

namespace Simhopp.Common
{
    [TestFixture]
    class TestDatabase
    {
        private User _judge1;
        private Diver _diver1;
        private Dive _dive1;
        private Score _score1;

        [SetUp]
        public void Init()
        {
            MyDbConnection.SetConnectionString(@"Data Source=DS9;Initial Catalog=Diving;Integrated Security=True;MultipleActiveResultSets=True");
            
            _judge1 = new User
            {
                Age = 32,
                Gender = Genders.Male,
                Name = "Kurt Olsson",
                Nationality = "Sweden",
                SocialSecurityNumber = "19640102-1412",
                UserName = "kurt",
                AccountType = AccountTypes.Judge
            };
            UserMap.SaveUserToDatabase(_judge1);

            _diver1 = new Diver
            {
                Age = 14,
                Gender = Genders.Female,
                Name = "Lena Andersson",
                Nationality = "Norway",
                SocialSecurityNumber = "19560102-1412"
            };
            DiverMap.SaveDiverToDatabase(_diver1);

            _dive1 = new Dive
            {
                KeyId = 23,
                DiverId = 14
            };
            DiveMap.SaveDiveToDatabase(_dive1);

            _score1 = new Score
            {
                JudgeId = 23,
                Value = 14.5f
            };
            ScoreMap.SaveScoreToDatabase(_score1);
        }


        [Test]
        public void TestReadJudge()
        {
            var tmpJudge = UserMap.ReadJudgeFromDataBase(_judge1.Id);
            tmpJudge.Credentials = CredentialsMap.ReadCredentialsFromDataBase(_judge1.CredentialsId);
            Assert.AreEqual(_judge1.Id, tmpJudge.Id);
            Assert.AreEqual(_judge1.Name, tmpJudge.Name);
            Assert.AreEqual(_judge1.Age, tmpJudge.Age);
            Assert.AreEqual(_judge1.Gender, tmpJudge.Gender);
            Assert.AreEqual(_judge1.Nationality, tmpJudge.Nationality);
            Assert.AreEqual(_judge1.SocialSecurityNumber, tmpJudge.SocialSecurityNumber);
            Assert.AreEqual(_judge1.UserName, tmpJudge.UserName);
        }

        [Test]
        public void TestRemoveJudge()
        {
            UserMap.RemoveUserFromDataBase(_judge1);
            var tmpJudge = UserMap.ReadJudgeFromDataBase(_judge1.Id);
            Assert.AreEqual(tmpJudge, null);
        }

        [Test]
        public void TestReadDiver()
        {
            var tmpDiver = DiverMap.ReadDiverFromDatabase(_diver1.Id);
            tmpDiver.Credentials = CredentialsMap.ReadCredentialsFromDataBase(_diver1.CredentialsId);
            Assert.AreEqual(_diver1.Id, tmpDiver.Id);
            Assert.AreEqual(_diver1.Name, tmpDiver.Name);
            Assert.AreEqual(_diver1.Age, tmpDiver.Age);
            Assert.AreEqual(_diver1.Gender, tmpDiver.Gender);
            Assert.AreEqual(_diver1.Nationality, tmpDiver.Nationality);
            Assert.AreEqual(_diver1.SocialSecurityNumber, tmpDiver.SocialSecurityNumber);
        }

        [Test]
        public void TestRemoveDiver()
        {
            DiverMap.RemoveDiverFromDatabase(_diver1);
            var tmpDiver = DiverMap.ReadDiverFromDatabase(_diver1.Id);
            Assert.AreEqual(tmpDiver, null);
        }

        [Test]
        public void TestReadDive()
        {
            var tmpDive = DiveMap.ReadDiveFromDatabase(_dive1.Id);
            Assert.AreEqual(_dive1.Id, tmpDive.Id);
            Assert.AreEqual(_dive1.DiverId, tmpDive.DiverId);
            Assert.AreEqual(_dive1.KeyId, tmpDive.KeyId);
        }

        [Test]
        public void TestRemoveDive()
        {
            DiveMap.RemoveDiveFromDatabase(_dive1);
            var tmpDive = DiveMap.ReadDiveFromDatabase(_dive1.Id);
            Assert.AreEqual(tmpDive, null);
        }

        [Test]
        public void TestReadScore()
        {
            var tmpScore = ScoreMap.ReadScoreFromDatabase(_score1.Id);
            Assert.AreEqual(_score1.Id, tmpScore.Id);
            Assert.AreEqual(_score1.JudgeId, tmpScore.JudgeId);
            Assert.AreEqual(_score1.Value, tmpScore.Value);
        }

        [Test]
        public void TestRemoveScore()
        {
            ScoreMap.RemoveScoreFromDatabase(_score1);
            var tmpScore = ScoreMap.ReadScoreFromDatabase(_score1.Id);
            Assert.AreEqual(tmpScore, null);
        }

        [TearDown]
        public void Terminate()
        {
            UserMap.RemoveUserFromDataBase(_judge1);
            DiverMap.RemoveDiverFromDatabase(_diver1);
            DiveMap.RemoveDiveFromDatabase(_dive1);
            ScoreMap.RemoveScoreFromDatabase(_score1);
        }
    }
}
